/*
 * jxUtils Project (2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxUtils.system;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

import org.jxUtils.check.CheckUtil;
import org.jxUtils.check.ParameterCheck;

import sun.net.util.IPAddressUtil;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxUtils
 * @package org.jxUtils.system
 * @date 15/11/2014 - 19:51:56
 */
public final class NetworkUtil {
	
	/**
	 * @return Canonical Host Name
	 */
	public static String getCanonicalHostName() {
	
		try {
			return InetAddress.getLocalHost().getCanonicalHostName();
			
		}catch(Throwable throwable) {
			
			return "";
		}
	}
	
	/**
	 * @return Host Address (IPv4 or IPv6)
	 */
	public static String getHostAddress() {
	
		try {
			return InetAddress.getLocalHost().getHostAddress();
			
		}catch(Throwable throwable) {
			
			return "";
		}
	}
	
	/**
	 * @return Host Name
	 */
	public static String getHostName() {
	
		try {
			return InetAddress.getLocalHost().getHostName();
			
		}catch(Throwable throwable) {
			
			return "";
		}
	}
	
	/**
	 * @return MAC Address
	 */
	public static String getMACAddress() {
	
		String address = "";
		
		try {
			//
			Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
			
			//
			while(networkInterfaces.hasMoreElements()) {
				
				NetworkInterface networkInterface = networkInterfaces.nextElement();
				
				byte[] hardwareAddress = networkInterface.getHardwareAddress();
				
				//
				if(CheckUtil.isNotNull(hardwareAddress)) {
					
					//
					for(byte b : hardwareAddress) {
						
						// 
						if( !address.trim().isEmpty()) {
							
							address += ":";
						}
						
						address += String.format("%02X", b);
					}
					
					break;
				}
			}
			
		}catch(Throwable throwable) {
			
			address = "";
		}
		
		return address;
	}
	
	/**
	 * @param value
	 * @return true if value is a valid IPv4
	 */
	public static boolean isIPv4(String value) {
	
		ParameterCheck.isNullOrAbsoluteEmpty(value, "ipv4");
		
		final String host = value.trim().replace("\\", "#").replace("/", "#").split("#")[0];
		
		final boolean result = IPAddressUtil.isIPv4LiteralAddress(host) || host.equalsIgnoreCase("localhost");
		
		//
		if( !result) {
			
			try {
				return InetAddress.getByName(host).isReachable(5000);
				
			}catch(final Throwable throwable) {
				// ignore
			}
		}
		
		return result;
	}
}
